package hai.com.android_test.widget.calendarView;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import hai.com.android_test.R;
import hai.com.android_test.adapter.MyBaseAdapter;
import hai.com.android_test.widget.AutoGridView;

/**
 * Created by 黄海 on 2017/6/22.
 */

public class MonthFragment extends Fragment {
    int index = -1;
    private static final String TAG = "MonthFragment";
    @BindView(R.id.gridView)
    AutoGridView gridView;
    private int daysOfMonth;
    private int firstDayOfWeek;
    private ArrayList<Integer> datas = new ArrayList<>(0);
    private BaseAdapter adapter;
    private Calendar currentDate = Calendar.getInstance();
    View view;

    public MonthFragment(int index) {
        this.index = index;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView() called with: index = [" + index);
        if (null == view) {
            view = inflater.inflate(R.layout.item_calendar, container, false);
            ButterKnife.bind(this, view);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated() called with: index = [" + index);
        gridView.setAdapter(adapter = new MyBaseAdapter<Integer>(getContext(), datas) {
            @Override
            public int getCount() {
                return 42;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_day, null);
                }
                TextView tv = (TextView) convertView.findViewById(R.id.tv_day);
                int num = position - firstDayOfWeek + 2;
                if (num < 1 || num > daysOfMonth) tv.setText(null);
                else tv.setText(String.valueOf(num));
                return convertView;
            }
        });
        updateDate(currentDate);
    }

    public void updateDate(Calendar dst_date) {
        if (dst_date != null && currentDate.compareTo(dst_date) != 0) {
            currentDate = (Calendar) dst_date.clone();
            firstDayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK);
            daysOfMonth = currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (adapter != null) adapter.notifyDataSetChanged();
        }
    }

    public Calendar getCurrentDate() {
        return (Calendar) currentDate.clone();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.i(TAG, "onAttach() called with: index = [" + index);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate() called with: index = [" + index);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart() called with: index = [" + index);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume() called with: index = [" + index);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated() called with: index = [" + index);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause() called with: index = [" + index);
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(TAG, "onStop() called with: index = [" + index);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy() called with: index = [" + index);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ((ViewGroup) view.getParent()).removeView(view);
        Log.i(TAG, "onDestroyView() called with: index = [" + index);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(TAG, "onDetach() called with: index = [" + index);
    }
}
